﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;

namespace LicPdaService
{
    public class Login
    {
        public static string GetUser(string xml)
        {
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            try
            {
                //-------接收XML处理-------
                DataTable ldt1 = null;
                try
                {
                    ldt1 = App.Xml2DataTable(xml);
                    if (ldt1.Rows.Count == 0)
                    {
                        return "";
                    }
                }
                catch (Exception ex)
                {
                    App.Log("XML转DT失败", ex.Message.ToString());
                    throw;
                }

                DataTable ldt = null;
                try
                {
                    strSql = "select userid, password,roleid from UserInfo where userid='" + ldt1.Rows[0]["username"].ToString() + "' and password='" + ldt1.Rows[0]["pwd"].ToString() + "'";
                    ldt = App.GetDataTable(App.DBconWMS, strSql, "UserInfo");
                }
                catch (Exception ex)
                {
                    App.Log("LOGIN-GetTabel", ex.Message.ToString());
                    throw;
                }
                //-------返回XML处理-------
                XmlDocument doc = new XmlDocument();//返回报文
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                XmlElement rootRet = doc.CreateElement("NewDataSet");
                XmlNode userNodesRet = doc.CreateElement("login");//返回用户节点
                XmlElement lxeWhseid = doc.CreateElement("whseid");
                XmlElement lxeUserName = doc.CreateElement("username");
                XmlElement lxePwd = doc.CreateElement("pwd");
                XmlElement lxeCustomer_Code = doc.CreateElement("customer_code");
                XmlElement lxeVendor = doc.CreateElement("vendor");
                if (ldt.Rows.Count > 0) //登录成功
                {
                    lxeWhseid.InnerText = "wh1";
                    lxeUserName.InnerText = ldt.Rows[0]["userid"].ToString();
                    lxePwd.InnerText = ldt.Rows[0]["password"].ToString();
                    lxeCustomer_Code.InnerText = "";
                    lxeVendor.InnerText = "";
                    userNodesRet.AppendChild(lxeWhseid);
                    userNodesRet.AppendChild(lxeUserName);
                    userNodesRet.AppendChild(lxePwd);
                    userNodesRet.AppendChild(lxeCustomer_Code);
                    userNodesRet.AppendChild(lxeVendor);
                }
                else //登录失败
                {
                    lxeWhseid.InnerText = ""; //前台根据whseid为空 判定为登录失败
                    lxeUserName.InnerText = "登录失败";
                    lxePwd.InnerText = "登录失败";
                    lxeCustomer_Code.InnerText = "";
                    lxeVendor.InnerText = "";
                    userNodesRet.AppendChild(lxeWhseid);
                    userNodesRet.AppendChild(lxeUserName);
                    userNodesRet.AppendChild(lxePwd);
                    userNodesRet.AppendChild(lxeCustomer_Code);
                    userNodesRet.AppendChild(lxeVendor);
                }

                rootRet.AppendChild(userNodesRet);
                doc.AppendChild(rootRet);
                return doc.InnerXml;
            }
            catch (Exception ex)
            {
                App.Log("Login", ex.Message.ToString());
                throw;
            }

        }
    }
}
